/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.testcommons;

import java.util.ServiceLoader;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;

public abstract class InjectableTestCase extends AbstractModule {
 
	private final Injector	injector;

	/**
	 * Constructor loads modules via JDK services locator.
	 */
	public InjectableTestCase() {
		ServiceLoader<Module> serviceLoader = ServiceLoader
				.load(com.google.inject.Module.class);
		for (Module module : serviceLoader) {
			System.out.println(module);
		}
		 
		injector = Guice.createInjector(serviceLoader);
		injector.injectMembers(this);
	}

	public InjectableTestCase(Module... modules) {
		injector = Guice.createInjector(modules);
		injector.injectMembers(this);
	}
 

	protected <T> T getInstance(final Class<? extends T> clazz) {
		return injector.getInstance(clazz);
	}

	protected <T> T getInstance(final TypeLiteral<? extends T> type) {
		return injector.getInstance(Key.get(type));
	}

	protected <T> T getInstance(final Key<? extends T> key) {
		return injector.getInstance(key);
	}
	
	@Override
	protected void configure() {
		// stub implementnation. Override for class specific implementations.
		
	}
	
}
